--
-- Author: shunguo.chen
-- Date: 2017-03-27 14:38:27
--
-- BagPgActView
--

local Armature = app:getClass("Armature")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.BagPgActView

function ClassRef:init()
	display.uiAddClick(self._ui.bg_0, function()
		if self._canStop then
			if self._playSuccessScheduler then	--防止中途取消分解计时器没有被销毁
				scheduler.cancelCron(self._playSuccessScheduler)
				self._playSuccessScheduler = nil
			end

			executeFunc(self._onCloseView)
		end
	end)

	self._canStop = true

	self:startCountDown()
end

function ClassRef:startCountDown()
	self._ui.Loading:setVisible( false ) -- 进度条原件
	self._ui.tlt:setString(self._data.op == "breakItem" and L("text_10400018") or L("function_name_identity"))
	local countDownAction = cc.ProgressTo:create(1, 100)
	local countDownSprite = cc.ProgressTimer:create( self._ui.Loading )
	countDownSprite:setName( "countDownSprite" )
	countDownSprite:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
	countDownSprite:setMidpoint( cc.p( 0.5, 0.5 ) )
	countDownSprite:setPosition( self._ui.Loading:getPosition() )
	self._ui.group:addChild( countDownSprite )

	local function countDownEnd()
        self:playAnim(self._data.multiBreak)
	end
	local action = cc.Sequence:create(countDownAction, cc.CallFunc:create(countDownEnd))
	countDownSprite:runAction(action)
end

-- 分解特效
function ClassRef:playAnim(multiBreak)
	self._canStop = false
	self._ui.text:setVisible(false)
	local params = {
		armatureName = "fenjieMC2",
		path = Res.BreakItemEfcPath
	}
	if multiBreak then -- 批量分解？
		params.armatureName = "fenjieMC1"
	end
	local armature = Armature.create( params )
	self._ui.group:addChild( armature )
	armature:setPosition( self._ui.Loading:getPosition() )

	armature:setVisible(true)
	armature:play("release", 1, function(sender)
		self._ui.group:removeChildByName( "countDownSprite" )
		executeFunc(self._onOperate, self._data.op, {self._data.breakType, self._data.uids, self._data.num})
	end)
end

-- 分解成功特效
function ClassRef:playSuccessAnim()
    self._playAniRemainTime = #self._data.dropNum
    local function _tick()
        if self._playAniRemainTime > 0 then
            local i = #self._data.dropNum - self._playAniRemainTime + 1
            local str = string.format(L("lua_code_text_422"), self._data.dropNum[i] or 0)
            local panel = self._ui.sum:getChildByName("Panel")
            local num = panel:getChildByName("num")
            local gold = panel:getChildByName("gold")
            if self._data.dropItem then
                gold:loadTexture(self._data.dropItem[i])
            end
            num:setString(str)

            display.setNodesMiddle(panel, nil, num, gold)

            self._ui.sum:setVisible(true)
            self._ui.group:setVisible(false)
            self:playAction(nil, false)

            self._playAniRemainTime = self._playAniRemainTime - 1
        else
            if self._playSuccessScheduler then
                scheduler.cancelCron(self._playSuccessScheduler)
                self._playSuccessScheduler = nil
            end
			executeFunc(self._onCloseView)
        end
    end
    self._playSuccessScheduler = scheduler.newCron(_tick, 0.6, true)
    _tick()
end

return ClassRef
